package yunplus.io.hchome.model.storage;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

import yunplus.io.hchome.display.widget.PreviewWebview;


public final class LoginShared {

    private LoginShared() {}

    private static final String TAG = "LoginShared";

    public static final String KEY_ACCESS_TOKEN = "token";
    public static final String KEY_ID = "id";
    public static final String KEY_LOGIN_NAME = "username";
    public static final String KEY_LOGIN_PASS = "password";
    public static final String KEY_DOMAIN = "domain";

    private static String accessToken;
    private static String id;
    private static String loginName;
    private static String loginPass;
    private static String domain;
    private static JSONObject user;

    public static void login(@NonNull Context context, @NonNull String accessToken, @NonNull JSONObject loginInfo) {
        SharedWrapper sharedWrapper = SharedWrapper.with(context, TAG);
        sharedWrapper.setString(KEY_ACCESS_TOKEN, accessToken);
        sharedWrapper.setString(KEY_ID, loginInfo.getString(KEY_ID));
        sharedWrapper.setString(KEY_LOGIN_NAME, loginInfo.getString(KEY_LOGIN_NAME));
        sharedWrapper.setString(KEY_LOGIN_PASS, loginInfo.getString(KEY_LOGIN_PASS));
        sharedWrapper.setString(KEY_DOMAIN, loginInfo.getString(KEY_DOMAIN));
        LoginShared.accessToken = accessToken;
        LoginShared.id = loginInfo.getString(KEY_ID);
        LoginShared.loginName = loginInfo.getString(KEY_LOGIN_NAME);
        LoginShared.loginPass = loginInfo.getString(KEY_LOGIN_PASS);
        LoginShared.domain = loginInfo.getString(KEY_DOMAIN);
        if(user == null){
            user = new JSONObject();
        }else{
            user.clear();
        }
        user.putAll(loginInfo);
        user.put(KEY_ACCESS_TOKEN, accessToken);
    }

    public static void update(@NonNull Context context, @NonNull String loginPass) {
        SharedWrapper sharedWrapper = SharedWrapper.with(context, TAG);
        sharedWrapper.setString(KEY_LOGIN_PASS, loginPass);
        LoginShared.loginPass = loginPass;
    }

    public static void logout(@NonNull Context context) {
        SharedWrapper.with(context, TAG).clear();
        accessToken = null;
        id = null;
        loginName = null;
        loginPass = null;
        user = null;
    }

    public static String getAccessToken(@NonNull Context context) {
        if (TextUtils.isEmpty(accessToken)) {
            accessToken = SharedWrapper.with(context, TAG).getString(KEY_ACCESS_TOKEN, null);
        }
        return accessToken;
    }

    public static String getId(@NonNull Context context) {
        if (TextUtils.isEmpty(id)) {
            id = SharedWrapper.with(context, TAG).getString(KEY_ID, null);
        }
        return id;
    }

    public static String getLoginName(@NonNull Context context) {
        if (TextUtils.isEmpty(loginName)) {
            loginName = SharedWrapper.with(context, TAG).getString(KEY_LOGIN_NAME, null);
        }
        return loginName;
    }

    public static String getDomain(@NonNull Context context) {
        if (TextUtils.isEmpty(domain)) {
            domain = SharedWrapper.with(context, TAG).getString(KEY_DOMAIN, null);
        }
        return domain;
    }

    public static String getLoginPass(@NonNull Context context) {
        if (TextUtils.isEmpty(loginPass)) {
            loginPass = SharedWrapper.with(context, TAG).getString(KEY_LOGIN_PASS, null);
        }
        return loginPass;
    }

    public static JSONObject getUser(@NonNull Context context){
        if(user == null) {
            // not login
            getAccessToken(context);
            if(accessToken != null){
                user = new JSONObject();
                user.put(KEY_ID, getId(context));
                user.put(KEY_LOGIN_NAME, getLoginName(context));
                user.put(KEY_LOGIN_PASS, getLoginPass(context));
                user.put(KEY_ACCESS_TOKEN, accessToken);
                user.put(KEY_DOMAIN, getDomain(context));
                return user;
            }else{
                return null;
            }
        }else{
            return user;
        }
    }

}
